/********************************Copyright (c)**********************************\
**
 **                   (c) Copyright 2023, Main, China, QD.
 **                           All Rights Reserved
 **
 **                                By(wo4fisher)
 **                           http://www.wo4fisher.com
 **
 **----------------------------------文件信息------------------------------------
 ** 文件名称: usart.h
 ** 创建人员: wht
 ** 创建日期: 2023-12-19
 ** 文档描述:
 **
 **----------------------------------版本信息------------------------------------
 ** 版本代号: V0.1
 ** 版本说明: 初始版本
 **
 **------------------------------------------------------------------------------
 \********************************End of Head************************************/

#ifndef __USART_H_
#define __USART_H_
#include "ch32v20x.h"

/* Global define */
// ring buffer size
#define RING_BUFFER_LEN     (1024u)

// The length of a single buffer used by DMA
#define RX_BUFFER_LEN       (128u)

#define USART_RX_CH         DMA1_Channel5

/* Global Variable */
struct USART_DMA_CTRL_TypeDef {
    volatile uint8_t DMA_USE_BUFFER;
    uint8_t Rx_Buffer[2][RX_BUFFER_LEN];

};

struct Ring_Buffer_TypeDef {
    uint8_t buffer[RING_BUFFER_LEN];
    volatile uint16_t RecvPos;  //
    volatile uint16_t SendPos;  //
    volatile uint16_t RemainCount;

};

extern struct Ring_Buffer_TypeDef ring_buffer;

void USARTx_CFG(uint32_t baudrate);
void DMA_INIT(void);
void ring_buffer_push_huge(uint8_t *buffer, uint16_t len);
uint8_t ring_buffer_pop();
int usart1_send_data(char *buf, int size);
#endif

/********************************End of File************************************/
